#ifndef wfmysql_h
#define wfmysql_h


#include "mysql.h"
#include <string>
using std::string;

#include <sstream>
using std::stringstream;

#include <vector>
using std::vector;

#include <iostream>
using std::cout;
using std::endl;

#include <time.h>

///2018-1-10


struct WDictionary
{
	string key ;
	string value ;
	inline WDictionary(string k,string v):key(k),value(v){};
	WDictionary(string k,int v) ;
	WDictionary(string k,float v) ;
	WDictionary(string k,double v) ;
	inline WDictionary(){} ;
} ;



//////////////////////////////////////////////////

struct WDictGroup
{
	vector<WDictionary> kvVec ;
	inline void add( WDictionary& kv ){ kvVec.push_back(kv) ; };
} ;



//////////////////////////////////////////////////

struct WfRow
{
	
	inline WfRow(int nf):m_contentArray(0),m_numfields(nf){ m_contentArray=new string[m_numfields];} ;
	inline ~WfRow(){ if(m_contentArray) delete[] m_contentArray; m_contentArray=0;};
	string* m_contentArray ;
	inline int getInt(int index){ return atof(m_contentArray[index].c_str()); } ;
	inline double getDouble(int index){ return atof(m_contentArray[index].c_str()); } ;

	WfRow(const WfRow& ref)  ;
	int m_numfields;
	
} ;


struct WfMysql
{
	//basic methods
	~WfMysql();
	inline WfMysql():m_isconnected(false),m_conn(0){};
	inline bool isConnected(){ return m_isconnected;};
	bool connect(string host , string user,string pwd , string db, string port) ;
	int tableGetRows( string tbname , 
								string selstr ,  
								string wherestr , 
								string orderstr ,
								string limitstr ,
								vector<WfRow>& resultRows ) ;
	int tableGetCount( string tbname ,string wherestr ) ;
	bool tableInsert(  string tbname , WDictGroup& dictGroup ,int* pResultId) ;
	string current_datetimestr() ;


	int selectRows( 
					string sqlstr ,
					vector<WfRow>& resultRows ) ;


	//project related methods.
	bool projectHasRsData(int satid,int sensorid,int ptypeid, int resoid,int dtypeid, int proj, int qc , int ymd, int regionid) ;
	bool projectInsertRsData(int satid, int sensorid,int ptypeid, int resoid,int dtypeid, int projid, int qc ,int ymd, string fpath ,int regionid, int& rid) ;
	bool projectInsertNyRsData(int rsdataid,
		int rspdtid,
		int satid, int resoid,int dtid, int qc, int proj , string fpath, int ymd , int& rid) ;


protected:
	bool m_isconnected ;
	MYSQL* m_conn;

};

 
 

#endif